package com.example.gsp.mybatis.binding;

import com.example.gsp.mybatis.session.SqlSession;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.Collection;

public class MapperdProxy implements InvocationHandler {

    private SqlSession session;

    public MapperdProxy(SqlSession session) {
        this.session = session;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        Class<?> returnClass = method.getReturnType();
        if (Collection.class.isAssignableFrom(returnClass)) {
            return session.selectList(method.getDeclaringClass().getName() + "." + method.getName(), args == null ? null : args[0]);
        }
        return session.selectOne(method.getDeclaringClass().getName() + "." + method.getName(), args == null ? null : args[0]);
    }
}
